import axios from "axios";
import qs from 'qs';

axios.defaults.baseURL = 'http://www.520gongfu.com:9527/api'

// 添加请求拦截器
axios.interceptors.request.use(function(config) {
    // //console.log('config:', config);
    // 设置 请求 token 校验
    const token = sessionStorage.getItem('token')
    if (token) {
        config.headers['Authorization'] = token
    }
    // 在发送请求之前做些什么
    return config;
}, function(error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});


export const getcategoryData = () => {
    return axios.get('/category/data').then(res => {
        const { data } = res
        if (data.status == 0) {
            // //console.log(data.data);
            return data.data
        } else {
            Toast.fail(data.msg)
        }
    })
}

export const login = (params, callback) => {
    return axios.post('/user/login', qs.stringify(params)).then(res => {
        const { data } = res
        callback && callback(data)
    }).catch(err => {
        console.log(err);
    })
}

export const getProcuctList = (params) => {
    return axios.get('/product/list', {
        params
    }).then(res => {
        const { data } = res
        if (data.status == 0) {
            return data.data
        } else {
            Toast.fail(data.msg)
        }
    })
}

export const getProcuctDetail = (params) => {
        return axios.get('/product/detail', {
            params
        }).then(res => {
            const { data } = res
            if (data.status == 0) {
                return data.data
            } else {
                Toast.fail(data.msg)
            }
        })
    }
    //获取用户信息
export const getUserInfo = (callback) => {
    return axios.get('/user/info').then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

//推荐商品

export const getRecommend = (callback) => {
    return axios.get('/product/recommend').then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

//获取购物车数据
export const getCartList = (callback) => {
    return axios.get('/cart/list').then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })

}

//删除购物车数据
export const deleteCartItem = (params, callback) => {
    return axios.post('/cart/del', qs.stringify(params)).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

//添加购物车数据
export const cartAdd = (params, callback) => {
    return axios.post('/cart/add', qs.stringify(params)).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}

//商品列表信息
export const getProductList = (params, callback) => {
    return axios.get('/product/list', {
        // get请求参数配置
        params: params, // 参数
        // 序列化参数（格式化处理参数）
        paramsSerializer: function(params) {
            return qs.stringify(params)
        }
    }).then(res => {
        callback && callback(res.data)
    }).catch(err => {
        console.log(err);
    })
}